<?php

class CPath {

	public $path;
	
	const TEMP_IMG = "temp_img";
	
	const CER_IMG  = "cer_img";

	const HEAD_IMG = "head_img";

	const ITEM_IMG = "item_img";
	
	const CUT_ITEM_IMG = "cut_item_img";

	const FILE = "file";

	const CSS = "css";

	const PLUGIN = "plugin";

	const CUT_IMG = "cut_img";


	//得到的是絕對地址
	public function getPathFromType($type, $hasG = "/") {

		$yearmonth = date ( "Ym" );

		$uid = $_COOKIE[COOKIE_PRE.'uid'];

		switch ($type) {
			
			case "temp" :

				return MY_ROOT . "/html/upload/" . CPath::TEMP_IMG . "/{$yearmonth}/{$uid}{$hasG}";

				break;
				
			case "cer" :

				return MY_ROOT . "/html/upload/" . CPath::CER_IMG . "/{$yearmonth}/{$uid}{$hasG}";

				break;
				
			case "head" :

				return MY_ROOT . "/html/upload/" . CPath::HEAD_IMG . "/{$yearmonth}/{$uid}{$hasG}";

				break;

			case "item" :

				return MY_ROOT . "/html/upload/" . CPath::ITEM_IMG . "/{$yearmonth}/{$uid}{$hasG}";

				break;
				
			case "cut_item" :

				return MY_ROOT . "/html/upload/" . CPath::CUT_ITEM_IMG . "/{$yearmonth}/{$uid}{$hasG}";

				break;

			case "file" :

				return MY_ROOT . "/html/upload/" . CPath::FILE . "/{$yearmonth}/{$uid}{$hasG}";

				break;

			case "css" :

				return MY_ROOT . "/html/upload/" . CPath::CSS . "/{$yearmonth}/{$uid}{$hasG}";

				break;

			case "plugin" :

				return MY_ROOT . "/html/upload/" . CPath::PLUGIN . "/{$yearmonth}/{$uid}{$hasG}";

				break;

			case "cut" :

				return MY_ROOT . "/html/upload/" . CPath::CUT_IMG . "/{$yearmonth}/{$uid}{$hasG}";

				break;

			default :

				return "error";

		}


	}


	//得到的地址為了存入數據庫,$filename可以是文件名或帶文件名的路徑路徑
	public function toDbPath($type, $filename) {

		if (strstr ( $filename, '/' )) {

			$filenameArray = explode ( "/", $filename );
			
			$filename = array_pop($filenameArray);

		}

		$yearmonth = date ( "Ym" );

		$uid = $_COOKIE[COOKIE_PRE.'uid'];

		switch ($type) {

			case "temp" :

				return "html/upload/" . CPath::TEMP_IMG . "/{$yearmonth}/{$uid}/{$filename}";

				break;
				
			case "cer" :

				return "html/upload/" . CPath::CER_IMG . "/{$yearmonth}/{$uid}/{$filename}";

				break;
			
			case "head" :

				return "html/upload/" . CPath::HEAD_IMG . "/{$yearmonth}/{$uid}/{$filename}";

				break;

			case "item" :

				return "html/upload/" . CPath::ITEM_IMG . "/{$yearmonth}/{$uid}/{$filename}";

				break;
				
			case "cut_item" :

				return "html/upload/" . CPath::CUT_ITEM_IMG . "/{$yearmonth}/{$uid}/{$filename}";

				break;

			case "file" :

				return "html/upload/" . CPath::FILE . "/{$yearmonth}/{$uid}/{$filename}";

				break;

			case "css" :

				return "html/upload/" .  CPath::CSS . "/{$yearmonth}/{$uid}/{$filename}";

				break;

			case "plugin" :

				return "html/upload/" . CPath::PLUGIN . "/{$yearmonth}/{$uid}/{$filename}";

				break;

			case "cut" :

				return "html/upload/" . CPath::CUT_IMG . "/{$yearmonth}/{$uid}/{$filename}";

				break;

			default :

				return "error";

		}


	}


	//把從數據庫取出的地址轉成絕對地址

	public static function toAbsoluteUrlPath($databaseUrl) {

		$thisPath = MY_URL . "/" . $databaseUrl;
		
		return $thisPath;

	}

	public static function toAbsoluteRootPath($databaseUrl) {

		$thisPath = MY_ROOT . "/" . $databaseUrl;

		return $thisPath;

	}





}


